/**
 * create by young (young2f@outlook.com) on 2023/5/1
 *
 */
'use strict';
const base = require('../base/controllerBase');
const moment = require('moment');
module.exports = class extends base {
  async findDevices() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' }, organizationCode: { type: 'string' } };
      // console.log('findDevices body', body);
      ctx.validate(model, body);
      const { organizationCode, tenantCode } = body;
      const result = await this.app.dbs.find('Device', { tenantCode, organizationCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async bindDevice() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        organizationCode: { type: 'string' },
        devices: { type: 'array' }
      };
      ctx.validate(model, body);
      const result = await service.organizationService.bindDevice(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async unbindDevice() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' }, organizationCode: { type: 'string' } };

      ctx.validate(model, body);
      const result = await service.organizationService.unbindDevice(body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findMeters() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' }, organizationCode: { type: 'string' } };
      // console.log('findDevices body', body);
      ctx.validate(model, body);
      const { organizationCode, tenantCode } = body;
      const result = await this.app.dbs.find('Meter', { tenantCode, organizationCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async bindMeter() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        organizationCode: { type: 'string' },
        devices: { type: 'array' }
      };
      ctx.validate(model, body);
      const result = await service.organizationService.bindMeter(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async unbindMeter() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' }, organizationCode: { type: 'string' } };

      ctx.validate(model, body);
      const result = await service.organizationService.unbindMeter(body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

};
